const mongoose = require('mongoose');
const md5 = require('md5');

mongoose.connect('mongodb://localhost:27017/user_manage_1208');

mongoose.connection.on('open',async ()=>{

    // 初始化数据
    // 导入对象模型
    let userModel = require('./models/userModel');
    // 通过对象模型查找数据库中是否存在username === 'admin'的数据
    let adminUser = await userModel.findOne({username:"admin"});
    // console.log(adminUser);
    if(!adminUser){
       // 创建管理员账号
       userModel.create({
           username:"admin",
           pwd:md5('111111'),
           profile:"/upload/admin.png"
       }) 

       for(let i=0;i<30;i++){
            userModel.create({
                username:"user_"+i,
                pwd:md5('111111'),
                profile:"/upload/admin.png"
            }) 
       }
    }

    console.log("数据库连接成功");
})
mongoose.connection.on('error',()=>{
    console.log("数据库连接失败");
})

module.exports =mongoose;